/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.modules.api.controller.NewsController.java
 * <p>
 * 类产生时间: 2014-6-19 0019 下午 16:00
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package com.oceansoft.mobile.econsole.modules.api.controller;

import com.oceansoft.mobile.econsole.common.util.FormData;
import com.oceansoft.mobile.econsole.common.util.MD5Util;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.channel.entity.Channel;
import com.oceansoft.mobile.econsole.modules.news.entity.News;
import com.oceansoft.mobile.econsole.modules.news.service.INewsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 咨询相关内容控制器
 *
 * @author: chenw
 * @time: 2014-6-19 0019 下午 16:00
 */
@Controller
@RequestMapping("api")
public class ApiNewsController extends BaseController {

    @Resource
    private INewsService newsService;

    /**
     * 加载新闻栏目信息
     *
     * @return Result
     */
    @RequestMapping(value = "channels", method = GET)
    public Result loadChannelList() {
        List<Channel> list = newsService.loadAllChannels();
        return new Result(Result.SUCC, list, "请求成功");
    }

    /**
     * 新闻数据采集
     *
     * @param formData 表单提交数据
     * @return Result
     */
    @RequestMapping(value = "news/post", method = POST, produces = "application/json")
    public Result saveNews(@RequestBody FormData formData) {
        String token = formData.getHeader().get("token");
        String appKey = formData.getHeader().get("key");
        News news = formData.getBody(News.class);
        String TEST_TOKEN = "7ccb2d5c6a1ce43b2609a980bff27375";
        if (!StringUtils.hasText(token) || !MD5Util.getMD5String(String.format("%s_%s", appKey, token)).equals(TEST_TOKEN)) {
            return new Result(Result.FAILURE, "口令不正确！");
        }
        return newsService.save(news);
    }

    /**
     * 新闻数据采集
     *
     * @param news 表单提交数据
     * @return Result
     */
    @RequestMapping(value = "news/add", method = POST, produces = "application/json")
    public Result saveNewsService(@RequestBody News news) {
//        String ip = getRequestHost();
//        ip = "127.0.0.1";
//        if (com.oceansoft.mobile.econsole.common.util.StringUtils.isBlank(ip) || "127.0.0.1".equals(ip)) {
//            return new Result(Result.FAILURE, "调用接口IP非法，请与管理员联系！");
//        }
        return newsService.save(news);
    }

    /**
     * 分页加载新闻栏目信息
     *
     * @param channelId 栏目ID
     * @param pageIndex 当前查询页码
     * @return Result
     */
    @RequestMapping(value = "news/list/{channel_id}/{page_index}", method = GET)
    public Result loadNewsByPage(@PathVariable("channel_id") int channelId, @PathVariable("page_index") int pageIndex) {
        return newsService.loadNewsByPage(channelId, pageIndex, getRequestUrl());
    }

    /**
     * 加载首页面图片新闻列表
     *
     * @param topSize 首页显示的图片新闻数量
     * @return Result
     */
    @RequestMapping(value = "news/top/{top_size}", method = GET)
    public Result loadTopNews(@PathVariable("top_size") int topSize) {
        return newsService.loadTopImgNews(topSize);
    }

    /**
     * 加载新闻详细内容
     *
     * @param newsId 新闻ID
     * @return Result
     */
    @RequestMapping(value = "news/{news_id}", method = GET)
    public Result loadNewsDetail(@PathVariable("news_id") long newsId) {
        return newsService.loadNewsById(newsId);
    }


    @RequestMapping(value = "viewNews/{newsId}")
    public ModelAndView viewNews(@PathVariable("newsId") long newsId, Model model) {
        model.addAttribute("newId", newsId);
        return new ModelAndView("news/viewNews");
    }


//    烟台项目 新

    /**
     * 进入新闻显示页面
     *
     * @param channel
     * @return
     */
    @RequestMapping(value = "gotoNewsPage/{channel}", method = GET)
    public ModelAndView gotoNewsPage(@PathVariable("channel") String channel, ModelMap modelMap) {
        modelMap.addAttribute("title", Objects.equals("xctb", channel) ? "协查通报" : "预警防范");
        modelMap.addAttribute("channel", channel);
        return new ModelAndView("weixin/news/news_list");
    }

    /**
     * 分页加载新闻信息
     *
     * @param pageIndex 当前查询页码
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "news/{channel}/{page_index}")
    public Result loadNewsByPage(@PathVariable("channel") String channel, @PathVariable("page_index") int pageIndex) {
        return newsService.loadNewsByPage(channel, pageIndex);
    }


    /**
     * 进如新闻详情页
     *
     * @param newsId
     * @param model
     * @return
     */
    @RequestMapping(value = "goToNewDetail/{channel}/{newsId}")
    public ModelAndView goToNewDetail(@PathVariable("channel") String channel, @PathVariable("newsId") String newsId, Model model) {
        model.addAttribute("title", Objects.equals("xctb", channel) ? "协查通报" : "预警防范");
        model.addAttribute("newId", newsId);
        return new ModelAndView("weixin/news/news_details");
    }

    /**
     * 新闻详情
     *
     * @param newsId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "newsDetail/{news_id}", method = GET)
    public Result loadNews(@PathVariable("news_id") String newsId) {
        return newsService.loadNewsById(newsId);
    }
}
